<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="800px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="100px">
          <el-form-item label="所属章节">
            <ChapterSelect
              :defaultValue="form.chapterId"
              :courseId="form.courseId"
              @emit="setChapterId"
            />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio label="1">理论课</el-radio>
              <el-radio label="2">实验课</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="课时" prop="classHour">
            <el-input-number
              v-model="form.classHour"
              :min="1"
              :max="1000"
              size="medium"
              label="课时"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="周次" prop="weeks">
            <el-input-number
              v-model="form.weeks"
              :min="1"
              :max="1000"
              size="medium"
              label="周次"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="授课日期" prop="lessonDate">
            <el-date-picker
              v-model="form.lessonDate"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="重点与难点" prop="keyPoint">
            <el-input v-model.trim="form.keyPoint"></el-input>
          </el-form-item>
          <el-form-item label="内容摘要" prop="content">
            <el-input v-model.trim="form.content"></el-input>
          </el-form-item>
          <el-form-item label="作业" prop="busyBook">
            <el-input v-model.trim="form.busyBook"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: {
    ChapterSelect: () => import('@/components/dataSelect/chapterSelect'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      form: {
        busyBook: '', //作业
        classHour: '1', //课时
        courseId: '', //课程id
        chapterId: '', //章节id
        content: '', //内容摘要
        extFiled: '', //扩展字段
        keyPoint: '', //重点与难点
        lessonDate: '', //授课日期
        lessonId: '', //授课计划id
        orderBy: 10, //排序
        type: '1', //类型1-理论课，2-实验课，3…
        weeks: '1', //周次
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },
    }
  },
  props: ['lessonId'],
  mounted() {
    this.$bus.$on('showDialogLessonPlanAdd', this.init)
  },
  beforeDestroy() {
    this.$bus.$off('showDialogLessonPlanAdd')
  },
  methods: {
    //显示弹窗
    init(row) {
      if (row) {
        this.form = { ...this.form, ...row }
        this.$set(this.form, 'id', row.id)
      } else {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      }
      this.title = row ? '编辑' : '新增'
      this.form.courseId = this.$route.query.courseId
      this.dialogVisible = true
    },
    submitForm(form) {
      this.form.lessonId = this.lessonId
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          let dispatchType =
            this.title == '新增' ? 'addEduLessonPlan' : 'updateEduLessonPlan'
          await this.$store.dispatch(dispatchType, this.form)
          this.successMsg('操作成功')
          this.$bus.$emit('refreshLessonPlanList')
          this.dialogVisible = false
        }
      })
    },
    resetForm(form) {
      this.$refs[form].resetFields()
    },
    setChapterId(v) {
      this.form.chapterId = v
    },
  },
}
</script>